namespace MyAddin
{
	using System;
	using System.Runtime.InteropServices;
    using System.Windows.Forms;
    using Microsoft.Office.Core;
    using Application = Microsoft.Office.Interop.Excel.Application;
    using Microsoft.Office.Interop.Excel;

    [Guid("37348531-906C-4A01-BDF5-BC922943DB62")]
    [ComVisible(true)]
    public interface IMyClass
    {
        void Initialise(ref object workbook);
        void ShowVersion();
        void AddSheet();
    }

    [Guid("EB1640D0-B0A1-4370-A3AE-3F7858A08218")]
    [ComVisible(true), ComDefaultInterface(typeof(IMyClass)), ClassInterface(ClassInterfaceType.None)]
	public class MyClass : IMyClass
	{

        Application excel;
        Workbook workbook;

        public void Initialise(ref object workbook)
        {
            this.workbook = (Workbook)workbook;
            this.excel = (this.workbook.Parent as Microsoft.Office.Interop.Excel.Application);
            CommandBarPopup myMenu = (CommandBarPopup)excel.CommandBars["Worksheet Menu Bar"].Controls.Add(MsoControlType.msoControlPopup, Type.Missing, Type.Missing, Type.Missing, true);
            myMenu.Caption = "MyAddin";
            CommandBarButton ctrl1 = (CommandBarButton)myMenu.Controls.Add(MsoControlType.msoControlButton, Type.Missing, Type.Missing, Type.Missing, true);
            ctrl1.Caption = "Test";
            ctrl1.OnAction = "Run_Test2";
            ctrl1.FaceId = 186;
            ctrl1.Click += new _CommandBarButtonEvents_ClickEventHandler( TestClick );
        }

        public void TestClick(CommandBarButton Ctrl, ref bool CancelDefault)
        {
            MessageBox.Show( "click" );
        }


        public void ShowVersion()
        {
            MessageBox.Show(
                (this.workbook.Parent as Microsoft.Office.Interop.Excel.Application).Version
            );
        }

        public void AddSheet()
        {
            if (this.excel.ActiveWorkbook == null) return;
            Worksheet NewSheet = (Worksheet)this.excel.ActiveWorkbook.Worksheets.Add();
            NewSheet.Range["A1:B100"].Value = "Fred";
        }
	}
}